using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Mall.Api.Filters;

namespace Mall.Api.Controllers
{
    /// <summary>
    /// 商品品类管理
    /// </summary>


    // [Authorize]

    [ApiController]
    [Route("class")]
    public class ClassController : ControllerBase
    {
        private IRepository<Class> _class;

        private readonly IConfiguration _configuration;

        public ClassController(IConfiguration configuration, IRepository<Class> classes)
        {
            _configuration = configuration;
            _class = classes;

        }


        /// <summary>
        /// 获取商品品类
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public dynamic Get()
        {


            // 获得所有满足条件的记录
            var classess = _class.Table.Where(x => x.IsDeleted == false).OrderByDescending(s => s.ClassPriority);


            var res = new
            {
                Code = 1000,
                Data = classess,
                Msg = "获取品类列表成功^_^"
            };
            return res;
        }


        /// <summary>
        /// 添加品类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public dynamic Post(NewClass model)
        {


            var className = model.ClassName.Trim();
            var classPriority = 0;
            var c = _class.Table.Where(c => c.ClassName == className).FirstOrDefault();


            if (c != null)
            {

                var tmp = new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "该品类已存在！"
                };
                return JsonHelper.Serialize(tmp);
            }

            if (string.IsNullOrEmpty(className))
            {
                var tmp = new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "品类名称不能为空，请确认后重试"
                };
                return JsonHelper.Serialize(tmp);
            }
            var classes = new Class
            {

                ClassName = className,
                ClassPriority = classPriority
            };

            _class.Insert(classes);

            var res = new
            {
                Code = 200,
                Data = new {
                    Class=classes
                },
                
                Msg = "添加品类成功！"
            };
            return JsonHelper.Serialize(res);

        }





        /// <summary>
        /// 根据指定id查找品类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var classeses = _class.GetById(id);

            if (classeses == null)
            {
                return new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定品类不存在"
                };

            }
            return new
            {
                Code = 200,
                Data = classeses,
                Msg = "获取相应品类成功^_^"
            };

        }







        /// <summary>
        /// 修改品类信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public dynamic Put(int id, NewClass model)
        {
            var classes = _class.GetById(id);


            if (classes != null)
            {

                classes.ClassName = model.ClassName;
                classes.ClassPriority = model.ClassPriority;
                classes.IsActived = model.IsActived;
                classes.IsDeleted = model.IsDeleted;


                classes.UpdatedTime = DateTime.Now;
                _class.Update(classes);
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = classes,
                    Msg = string.Format("你修改或删除的品类的id为：{0}，已经修改成功，请注意查收", id)
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的品类不存在，请确认后重试"
                });
            }
        }





        /// <summary>
        /// 真删除指定品类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            var classes = _class.GetById(id);
            if (classes != null)
            {
                _class.Delete(id);
                return new
                {
                    Code = 200,
                    Msg = string.Format("删除指定id为{0}的品类成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "指定Id的品类不存在，请确认后重试"
                };
            }

        }




    }
}